<template>
  <ul class="list">
    <li
      class= "item"
      v-for="(item,key) of cities"
      :key="item.id"
      @click="handleLetterClick"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      :ref="key"
    >{{key}}</li>
  </ul>
</template>
<script>
export default{
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        // console.log(i)
        letters.push(i)
      }
      return letters
    }
  },
  data () {
    return {
      status: false
    }
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart () {
      this.status = true
    },
    handleTouchMove (e) {
      let index
      setTimeout(() => {
        const startY = this.$refs['A'][0].offsetTop
        const touchY = e.touches[0].clientY - 79
        index = Math.floor((touchY - startY) / 20)
        console.log(touchY)
        if (index >= 0 && index < this.letters.length) {
          this.$emit('change', this.letters[index])
        }
        // console.log(this.$refs[this.letters[index]][0])
      }, 100)
    },
    handleTouchEnd () {
      this.status = false
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .list
    display flex
    position absolute
    top 1.60rem
    right 0
    bottom 0
    color $bgColor
    width .6rem
    flex-direction column
    align-items center
    justify-content center
    .item
      height .4rem
      line-height .4rem
    .letterColor
      background blue

</style>
